package com.lzx.iteam.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.lzx.iteam.R;
import com.lzx.iteam.bean.WeeklyDataBean;
import com.lzx.iteam.util.DateUtil;
import com.lzx.iteam.util.StringUtil;
import com.lzx.iteam.util.WeekUtil;
import com.lzx.iteam.widget.PinnedSectionListView;

import java.util.ArrayList;


/**
 * Created by Megan on 16/5/23.
 */
public class WeeklyMainListAdapter extends BaseAdapter implements PinnedSectionListView.PinnedSectionListAdapter {

    private LayoutInflater inflater;
    private Context mContext;
    private ArrayList<WeeklyDataBean> weeklyDataBeans;
    private String[] s = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"};


    public WeeklyMainListAdapter(Context context) {
        this.mContext = context;
        this.inflater = LayoutInflater.from(context);

    }

    public void bindData(ArrayList<WeeklyDataBean> list) {
        this.weeklyDataBeans = list;
        notifyDataSetChanged();

    }

    /**
     * 根据getItemViewType方法返回结果最终确定是否是标题行
     *
     * @param viewType
     * @return true:标题行   false:普通行
     */
    @Override
    public boolean isItemViewTypePinned(int viewType) {
        if (viewType == 1) {
            return true;
        } else
            return false;
    }

    /**
     * 确定哪一行是标题行
     *
     * @param position
     * @return 1：标题行   0：普通行
     */
    @Override
    public int getItemViewType(int position) {
        if (StringUtil.isEmpty(weeklyDataBeans.get(position).getWeeklyIds())) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getCount() {
        return weeklyDataBeans == null ? 0 : weeklyDataBeans.size();
    }

    @Override
    public Object getItem(int i) {
        return weeklyDataBeans == null ? null : weeklyDataBeans.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        WeeklyDataBean dataBean = weeklyDataBeans.get(position);
        if (convertView == null) {
            if (getItemViewType(position) == 1) {
                convertView = inflater.inflate(R.layout.weekly_main_list_year_item, null);
                viewHolder = new ViewHolder();
                viewHolder.tvYear = (TextView) convertView.findViewById(R.id.tv_weekly_main_list_item_year);
                convertView.setTag(viewHolder);
            } else {
                convertView = inflater.inflate(R.layout.weekly_main_list_item, null);
                viewHolder = new ViewHolder();
                viewHolder.ivImage = (ImageView) convertView.findViewById(R.id.iv_weekly_main_list_image);
                viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_weekly_main_list_title);
                viewHolder.tvWeek = (TextView) convertView.findViewById(R.id.tv_weekly_main_list_week);
                convertView.setTag(viewHolder);
            }


        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (getItemViewType(position) == 1) {
            viewHolder.tvYear.setText(dataBean.getWeeklyYear() + "年");
        } else {
            if (dataBean.getWeeklyMonth() != null) {
                switch (Integer.parseInt(dataBean.getWeeklyMonth())) {
                    case 1:
                        viewHolder.ivImage.setImageDrawable(mContext.getResources().getDrawable(R.drawable.week_january));
                        break;
                    case 2:
                        viewHolder.ivImage.setImageDrawable(mContext.getResources().getDrawable(R.drawable.week_february));
                        break;
                    case 3:
                        viewHolder.ivImage.setImageDrawable(mContext.getResources().getDrawable(R.drawable.week_march));
                        break;
                    case 4:
                        viewHolder.ivImage.setImageDrawable(mContext.getResources().getDrawable(R.drawable.week_april));
                        break;
                    case 5:
                        viewHolder.ivImage.setImageDrawable(mContext.getResources().getDrawable(R.drawable.week_may));
                        break;
                    case 6:
                        viewHolder.ivImage.setImageDrawable(mContext.getResources().getDrawable(R.drawable.week_june));
                        break;
                    case 7:
                        viewHolder.ivImage.setImageDrawable(mContext.getResources().getDrawable(R.drawable.week_july));
                        break;
                    case 8:
                        viewHolder.ivImage.setImageDrawable(mContext.getResources().getDrawable(R.drawable.week_august));
                        break;
                    case 9:
                        viewHolder.ivImage.setImageDrawable(mContext.getResources().getDrawable(R.drawable.week_september));
                        break;
                    case 10:
                        viewHolder.ivImage.setImageDrawable(mContext.getResources().getDrawable(R.drawable.week_october));
                        break;
                    case 11:
                        viewHolder.ivImage.setImageDrawable(mContext.getResources().getDrawable(R.drawable.week_november));
                        break;
                    case 12:
                        viewHolder.ivImage.setImageDrawable(mContext.getResources().getDrawable(R.drawable.week_december));
                        break;
                }
                viewHolder.tvWeek.setText(s[Integer.parseInt(dataBean.getWeeklyMonth())] + "月第" + s[Integer.parseInt(dataBean.getWeekCount())] + "周("+DateUtil.formatMonthDay(WeekUtil.getFirstDayOfWeek( WeekUtil.getWeekDateByCount(Integer.parseInt(dataBean.getWeeklyYear()), Integer.parseInt(dataBean.getWeeklyMonth()), Integer.parseInt(dataBean.getWeekCount()))))+"-"+DateUtil.formatMonthDay(WeekUtil.getLastDayOfWeek( WeekUtil.getWeekDateByCount(Integer.parseInt(dataBean.getWeeklyYear()), Integer.parseInt(dataBean.getWeeklyMonth()), Integer.parseInt(dataBean.getWeekCount()))))+")");
            }

        }
        return convertView;
    }

    class ViewHolder {
        TextView tvYear;
        ImageView ivImage;
        TextView tvTitle;
        TextView tvWeek;

    }
}
